/*
 * Copyright 2008 Pavel Byles (pavelbyles@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.pavco.caribbeanvisit.client.ui;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.event.MarkerClickHandler.MarkerClickEvent;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.HasName;
import com.pavco.caribbeanvisit.shared.models.Country;
import com.pavco.caribbeanvisit.client.event.CountryMarkerClickEvent;
import com.pavco.caribbeanvisit.client.eventhandlers.CountryMarkerClickEventHandler;

public class CountryMarker extends Marker implements HasName {
  private HandlerManager eventBus;
  private PavcoMap map;
  private Marker me;
  private String name;
  
  public CountryMarker(Country country) {
    super(LatLng.newInstance(country.getLatitude(), country.getLongitude()));
    //this.map = map;
    //this.eventBus = eventBus;
    this.me = this; // important so that i can know which one of me got clicked :)
    this.name = country.getName();
    //listenForClickEvent();
    
    /*this.addMarkerClickHandler(new MarkerClickHandler() {
      public void onClick(MarkerClickEvent markerClickEvent) {
        fireClickEvent(markerClickEvent);
      }
    });*/
  }
  
  /*private void fireClickEvent(MarkerClickEvent markerClickEvent) {
    CountryMarkerClickEvent cmce = new CountryMarkerClickEvent();
    cmce.setMarker((CountryMarker)markerClickEvent.getSender());
    cmce.setCountry(country);
    eventBus.fireEvent(cmce);
  }
  
  private void listenForClickEvent() {
    eventBus.addHandler(CountryMarkerClickEvent.TYPE, 
        new CountryMarkerClickEventHandler() {

      @Override
      public void onMarkerClicked(CountryMarkerClickEvent event) {
        if (event.getMarker() != me) {
          return;
        }
        
        MarkerSlideShow ms = new MarkerSlideShow(event.getCountry(), eventBus);
        ms.load("pavelbyles3");
        InfoWindow iw = map.getInfoWindow();
        iw.open(event.getMarker(), new InfoWindowContent(ms));
        InfoWindowContent iwc = new InfoWindowContent(ms);
        //InfoWindowContent iwc = new InfoWindowContent("Hello World");
        iwc.setMaxTitle(name);
        iw.open(event.getMarker(), iwc);
      }
    });
  }*/

  @Override
  public void setName(String name) {
    this.name = name;
  }

  @Override
  public String getName() {
    return name;
  }
  
  public void openInfoWindow() {
  }
}